package com.runen.infinitemovement.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.runen.infinitemovement.tool.CommonUtil;

/**
 * 通用dialog
 * <p>
 * 使用方式
 * <p>
 * BaseCustomDialog.Builder builder = new BaseCustomDialog.Builder(getActivity());
 * BaseCustomDialog dialog = builder
 * .style(R.style.dialogstyle)
 * .cancelTouchout(false)
 * .heightDimenRes(R.dimen.y320)        //不传，默认wrap_content
 * .widthDimenRes(R.dimen.x560)         //不传，默认match_parent
 * .addViewOnclick(R.id.cannel, listener)
 * .addViewOnclick(R.id.ok, listener)
 * .view(R.layout.dialog_share_friends)
 * .gravity(Gravity.BOTTOM)            //不传默认底部
 * .build();
 * dialog.show();
 */
public class BaseCustomDialog extends Dialog {

    private Context context;
    private int height;
    private int width;
    private boolean cancelTouchout;
    private int gravity;
    private View view;
    private RelativeLayout rl_content;

    private BaseCustomDialog(Builder builder) {
        super(builder.context);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        gravity = builder.gravity;
        view = builder.view;
    }


    private BaseCustomDialog(Builder builder, int resStyle) {
        super(builder.context, resStyle);

        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        gravity = builder.gravity;
        view = builder.view;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(view);

        setCanceledOnTouchOutside(cancelTouchout);
//        rl_content = view.findViewById(R.id.rl_content);
//        Animation animationIn = AnimationUtils.loadAnimation(context, R.anim.bottom_out);
//        rl_content.setVisibility(View.VISIBLE);
//        rl_content.startAnimation(animationIn);

        Window win = getWindow();
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.gravity = gravity;
        lp.height = height;
        lp.width = width;
        win.setAttributes(lp);
    }

    public static final class Builder {

        private Context context;
        private int height = WindowManager.LayoutParams.WRAP_CONTENT;
        private int width = WindowManager.LayoutParams.MATCH_PARENT;
        private boolean cancelTouchout;
        private View view;
        private int resStyle = -1;
        private int gravity = Gravity.BOTTOM;


        public Builder(Context context) {
            this.context = context;
        }

        public Builder view(int resView) {
            view = LayoutInflater.from(context).inflate(resView, null);
            return this;
        }

        public Builder view(View view) {
            this.view = view;
            return this;
        }

        public Builder heightpx(int val) {
            height = val;
            return this;
        }

        public Builder widthpx(int val) {
            width = val;
            return this;
        }

        public Builder heightdp(int val) {
            height = CommonUtil.dp2px(context, val);
            return this;
        }

        public Builder widthdp(int val) {
            width = CommonUtil.dp2px(context, val);
            return this;
        }

        public Builder heightDimenRes(int dimenRes) {
            height = context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }

        public Builder widthDimenRes(int dimenRes) {
            width = context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }

        public Builder style(int resStyle) {
            this.resStyle = resStyle;
            return this;
        }

        public Builder cancelTouchout(boolean val) {
            cancelTouchout = val;
            return this;
        }

        public Builder addViewOnclick(int viewRes, View.OnClickListener listener) {
            view.findViewById(viewRes).setOnClickListener(listener);
            return this;
        }

        public Builder gravity(int val) {
            gravity = val;
            return this;
        }


        public BaseCustomDialog build() {
            if (resStyle != -1) {
                return new BaseCustomDialog(this, resStyle);
            } else {
                return new BaseCustomDialog(this);
            }
        }
    }
}
